<?php

namespace app\admin\model;

use think\Model;

/**
 * 涨知识
 * @package app\admin\model
 */
class Article extends Model
{
    /**
     * 文章总数
     * @param array $where
     * @return int
     */
    public static function countArticle($where = [])
    {
        return self::where($where)
            ->where('status', '<>', -1)
            ->count();
    }

    /**
     * 本周文章数据
     * @return array
     */
    public static function getArticleWeek()
    {
        $data = self::fieldRaw("COUNT(*) total,create_time")
            ->whereWeek('create_time')
            ->where('status', '<>', -1)
            ->group('create_time')
            ->select();
        return $data ? $data->toArray() : [];
    }

    /**
     * 本月文章数据
     * @param array $where
     * @return array
     */
    public static function getArticleMonth($where = [])
    {
        $data = self::fieldRaw("COUNT(*) total,create_time")
            ->whereMonth('create_time')
            ->where('status', '<>', -1)
            ->where($where)
            ->group('create_time')
            ->select();
        return $data ? $data->toArray() : [];
    }

    /**
     * 本月文章数据1
     * @param $where
     * @param $startTime
     * @param $endTime
     * @return array
     */
    public static function getArticleMonth1($where, $startTime, $endTime)
    {
        $data = self::fieldRaw("COUNT(*) total,create_time")
            ->where($where)
            ->whereTime('create_time', '>=', $startTime)
            ->whereTime('create_time', '<', $endTime)
            ->group('create_time')
            ->where('status', '<>', -1)
            ->select();

        return $data ? $data->toArray() : [];
    }

    /**
     * 本月新增文章
     * @param array $where
     * @return int
     */
    public static function countArticleMonthCreate($where = [])
    {
        return self::where($where)
            ->where('status', '<>', -1)
            ->whereMonth('create_time')
            ->count();
    }

    /**
     * 文章列表
     * @param $where
     * @param $page
     * @param $limit
     * @return array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getArticleList($where, $page, $limit)
    {
        $data = self::where($where)
            ->where('status', '<>', -1)
            ->page($page, $limit)
            ->order('create_time', 'desc')
            ->select();

        return $data ? $data->toArray() : [];
    }

    /**
     * 文章列表统计
     * @param $where
     * @return int
     */
    public static function countArticleList($where)
    {
        return self::where($where)
            ->where('status', '<>', -1)
            ->count();
    }

    /**
     * 单文章数据
     * @param $where
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function findArticle($where)
    {
        $info = self::where($where)->find();
        return $info ? $info->toArray() : [];
    }

    /**
     * 更新文章数据
     * @param $data
     * @param $where
     * @return Article
     */
    public static function updateArticle($data, $where)
    {
        return self::update($data, $where);
    }

    /**
     * 添加文章数据
     * @param $data
     * @return Article|Model
     */
    public static function createArticle($data)
    {
        return self::create($data);
    }

    /**
     * 文章数据
     * @param $where
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getArticleData($where)
    {
        $data = self::where($where)
            ->where('status', '<>', -1)
            ->field('id,enroll_id,types')
            ->select();
        return $data ? $data->toArray() : [];
    }

}